package com.neoCalf.neoNotify.listeners;

import com.alibaba.fastjson.JSON;
import com.neoCalf.neoCommon.constants.Constants;
import com.neoCalf.neoCommon.enums.MqType;
import com.neoCalf.neoCommon.model.RabbitMqMessage;
import com.neoCalf.neoCommon.utils.AliYunSmsUtil;
import com.neoCalf.neoCommon.utils.CodeUtils;
import com.neoCalf.neoCommon.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/8 16:43
 */
@Slf4j
@Component
public class MqListener {
    @Autowired
    private CodeUtils codeUtils;

    // 使用背压控制的批量消费
    @RabbitListener(
            queues = "sendCheckCodeQueue",
            containerFactory = "containerFactory",
            concurrency = "5-10"
    )
    public void receiveMessage(String message) {
        RabbitMqMessage rabbitMqMessage = JSON.parseObject(message, RabbitMqMessage.class);
        if (rabbitMqMessage != null) {
            String to = (String) rabbitMqMessage.getMessage();
            MqType type = rabbitMqMessage.getType();
            String code;
            switch (type) {
                case SEND_PHONE_MESSAGE:
                    code = codeUtils.generateCode2(Constants.PHONE_CODE_KEY + to, Constants.PHONE_CODE_EXPIRE);
                    AliYunSmsUtil.sendCode(to, code);
                    break;
                case SEND_EMAIL_MESSAGE:
                    code = codeUtils.generateCode2(Constants.EMAIL_CODE_KEY + to, Constants.EMAIL_CODE_EXPIRE);
                    MailUtils.sendMailCheck(to, code);
                    break;
            }
        }
    }
}
